package com.jianzhile.jobs.activities.add;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.gugalor.adapter.BucketListAdapter;
import com.jianzhile.jobs.R;
import com.jianzhile.jobs.base.BaseActivity;
import com.jianzhile.jobs.bean.BucketEntity;
import com.jianzhile.jobs.utils.AlbumHelper;
import com.jianzhile.jobs.utils.Const;

import java.util.ArrayList;

/**
 * 相册选择 activity
 */
public class AlbumsChooserActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {

	private static final String TAG = "AlbumsChooserActivity";
	// 相册列表获取完成
	private static final int ALBUM_LIST_COMPLETE = 1;
	private TextView mPhotoTv;
	private TextView mTitleTv;
	private TextView mCancelTv;
	private ListView mAlbumListView;
	private BucketListAdapter mAdapter;
	private ArrayList<BucketEntity> mBucketList;
	// 被选中的相册数据list
	private ArrayList<String> mSelectedDataList = new ArrayList<String>();


	private void init() {
		mPhotoTv = (TextView) findViewById(R.id.photo_tv);
		mPhotoTv.setVisibility(View.GONE);
		mTitleTv = (TextView) findViewById(R.id.title_tv);
		mTitleTv.setText(R.string.choose_album);
		mCancelTv = (TextView) findViewById(R.id.cancel_tv);

		mAlbumListView = (ListView) findViewById(R.id.albums_listview);
	}

	private void initEvents() {
		mCancelTv.setOnClickListener(this);
		mAlbumListView.setOnItemClickListener(this);
	}

	/**
	 * 加载album的数据
	 */
	private void loadAlbum() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				mBucketList = AlbumHelper.getBucketList(
						AlbumsChooserActivity.this, getContentResolver());

				mHandler.sendEmptyMessage(ALBUM_LIST_COMPLETE);
			}
		}).start();
	}

	/**
	 * 初始化选择相册列表
	 */
	private void initDisplay() {
		mAdapter = new BucketListAdapter(this, mBucketList);
		mAlbumListView.setAdapter(mAdapter);
	}

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case ALBUM_LIST_COMPLETE:
				// 初始化列表显示
				initDisplay();
				break;

			default:
				break;
			}
		}

	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel_tv:
			this.finish();
			this.overridePendingTransition(0, R.anim.push_top_out);
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
							long id) {
		BucketEntity entity = (BucketEntity) parent.getAdapter().getItem(
				position);
		if (null != entity) {
			launchAlbumActivity(entity);
		}
	}

	private void launchAlbumActivity(BucketEntity entity) {
		Intent intent = new Intent();
		intent.putExtra(Const.BUCKET_ID_EXTRA, entity.getBucketId());
		intent.putExtra(Const.SELECTED_PATH_EXTRA, mSelectedDataList);
		intent.putExtra(Const.TITLE_EXTRA, entity.getBucketName());
		setResult(Activity.RESULT_OK, intent);
		this.finish();
		this.overridePendingTransition(0, R.anim.push_top_out);
	}

	@Override
	public void initView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_albums_chooser);
		init();
		initEvents();
		// 加载album数据
		loadAlbum();
	}

	@Override
	public void setListener() {

	}

	@SuppressWarnings("unchecked")
	@Override
	public void initData() {
		Intent intent = getIntent();
		if (null != intent) {
			mSelectedDataList = (ArrayList<String>) intent
					.getSerializableExtra(Const.SELECTED_PATH_EXTRA);
		}
	}

}
